package com.zy.conversation.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ContractController {

    private boolean requestReceived = false;
    private boolean contractCreated = false;
    private String tenantName = "";
    private String landlordName = "";

    @PostMapping("/tenant/request")
    public String tenantRequest(@RequestBody Map<String, String> payload) {
        this.tenantName = payload.get("name");
        requestReceived = true;
        return "Request sent to landlord";
    }

    @GetMapping("/landlord/response")
    public String landlordResponse(@RequestParam boolean agree, @RequestParam String name) {
        if (requestReceived) {
            this.landlordName = name;
            contractCreated = agree;
            requestReceived = false;
            return agree ? "Contract agreed" : "Contract declined";
        }
        return "No request received";
    }

    @GetMapping("/contract/status")
    public String contractStatus() {
        return contractCreated ? "Contract created" : "No contract created";
    }

    @GetMapping("/tenant/request/status")
    public Map<String, Boolean> getRequestStatus() {
        Map<String, Boolean> status = new HashMap<>();
        status.put("requestReceived", requestReceived);
        return status;
    }

    @GetMapping("/contract/details")
    public Map<String, Object> getContractDetails() {
        Map<String, Object> details = new HashMap<>();
        details.put("contractCreated", contractCreated);
        details.put("tenantName", tenantName);
        details.put("landlordName", landlordName);
        return details;
    }
}
